package com.ywj.interview.thread.stopThread;

public class ThreadFlag extends Thread{
    public volatile boolean exit = false;

    @Override
    public void run() {

        while (!exit) {
            System.out.println("ThreadFlag running ...");
            //do something
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadFlag t1 = new ThreadFlag();
        t1.start();
        sleep(2000);   //主线程延迟2秒
        t1.exit = true; //终止线程t1

        t1.join();
        System.out.println("线程退出");
    }
}
